package info.niwota.start.app;

import info.niwota.start.R;
import info.niwota.start.ResourceInfo;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppListAdapter extends BaseAdapter {

	protected static class ViewHolder {
			public ImageView icon;
			public TextView title;
			public TextView desc;
		}

	private ArrayList<ResourceInfo> list;
	protected Context ctx;
	
	public AppListAdapter(Context ctx, ArrayList<ResourceInfo> list) {
		this.ctx = ctx;
		this.list = list;
	}

	public long getItemId(int position) {
		return position;
	}
	
	public ResourceInfo getItem(int position) {
		if (list == null) {
			return null;
		}
		return list.get(position);
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater li = LayoutInflater.from(ctx);
			convertView = li.inflate(R.layout.app_list_item, null);
			holder = new ViewHolder();

			holder.title = (TextView) convertView.findViewById(R.id.item_title);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ResourceInfo item = getItem(position);

		holder.title.setText(item.getTitle());
		holder.icon.setImageDrawable(item.getIcon());
	
		return convertView;
	}

	public int getCount() {
		return (list == null ? 0 : list.size());
	}
}